package com.leyou.item.controller;


import com.leyou.item.Service.SpecificationService;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    //根据分类id获取规格参数组
    @GetMapping("groups/{cid}")
    //@PathVariable是路径必带的参数 对应上面的{cid}
    public ResponseEntity<List<SpecGroup>> querySpecGroupByCid(@PathVariable("cid") Long cid){

        List<SpecGroup> specGroups = this.specificationService.querySpecGroupByCid(cid);
        if (CollectionUtils.isEmpty(specGroups)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specGroups);
    }

    //根据规格组gid获取所有参数
//    @GetMapping("params")
    //@PathVariable是路径必带的参数 对应上面的{cid}
//    public ResponseEntity<List<SpecParam>> querySpecGroupByGid(@RequestParam("gid") Long gid){
//
//        List<SpecParam> specParams = this.specificationService.queryParamsByGid(gid);
//        if (CollectionUtils.isEmpty(specParams)){
//            return ResponseEntity.notFound().build();
//        }
//        return ResponseEntity.ok(specParams);
//    }


    //根据前端品牌新增的结构表达式参数 提供所需的规格参数
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParams(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "generic",required = false) Boolean generic,
            @RequestParam(value = "searching",required = false) Boolean searching
    ){

        List<SpecParam> specParams = this.specificationService.queryParams(gid,cid,generic,searching);
        if (CollectionUtils.isEmpty(specParams)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specParams);
    }


    @GetMapping("{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecsByCid(@PathVariable("cid") Long cid){
        List<SpecGroup> list = this.specificationService.querySpecsByCid(cid);
        if(list == null || list.size() == 0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }
}
